package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblRemoteInterfaceInfoMapper;
import com.showsu.editor.entity.doo.RemoteInterfaceInfo;
import com.showsu.editor.entity.po.TblRemoteInterfaceInfo;
import com.showsu.editor.service.IRemoteInterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 7:55 下午 2023/11/23
 * @Modified By:
 */
@Service
@Slf4j
public class RemoteInterfaceInfoServiceImpl extends ServiceImpl<TblRemoteInterfaceInfoMapper, TblRemoteInterfaceInfo> implements IRemoteInterfaceInfoService {

    @Autowired
    private TblRemoteInterfaceInfoMapper tblRemoteInterfaceInfoMapper;


    @Override
    public RemoteInterfaceInfo queryUsingInterfaceInfo(String interfaceName) {
        if (StringUtils.isBlank(interfaceName)) {
            return null;
        }
        TblRemoteInterfaceInfo tblRemoteInterfaceInfo = tblRemoteInterfaceInfoMapper.selectOne(new QueryWrapper<TblRemoteInterfaceInfo>().lambda()
                .eq(TblRemoteInterfaceInfo::getInterfaceName, interfaceName)
                .eq(TblRemoteInterfaceInfo::getActive, 1)
                .last("limit 1"));
        return tblRemoteInterfaceInfo == null ? null : BeanUtil.copyProperties(tblRemoteInterfaceInfo, RemoteInterfaceInfo.class);
    }

    @Override
    public RemoteInterfaceInfo queryById(Long interfaceId) {
        if (interfaceId == null) {
            return null;
        }
        TblRemoteInterfaceInfo tblRemoteInterfaceInfo = tblRemoteInterfaceInfoMapper.selectOne(new QueryWrapper<TblRemoteInterfaceInfo>().lambda()
                .eq(TblRemoteInterfaceInfo::getInterfaceId, interfaceId));
        return tblRemoteInterfaceInfo == null ? null : BeanUtil.copyProperties(tblRemoteInterfaceInfo, RemoteInterfaceInfo.class);
    }

    @Override
    public List<RemoteInterfaceInfo> queryAutoSwitchInterfacesBySameInterface(Long interfaceId) {
        if (interfaceId == null) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("interfaceId", interfaceId);
        List<TblRemoteInterfaceInfo> tblRemoteInterfaceInfoList = tblRemoteInterfaceInfoMapper.queryAutoSwitchInterfacesBySameInterface(params);
        return CollectionUtils.isEmpty(tblRemoteInterfaceInfoList) ? null
                : tblRemoteInterfaceInfoList.stream().map(
                tblRemoteInterfaceInfo -> { return BeanUtil.copyProperties(tblRemoteInterfaceInfo, RemoteInterfaceInfo.class); }
                        ).collect(Collectors.toList());
    }

    @Override
    public List<RemoteInterfaceInfo> queryAutoSwitchInterfacesByName(String interfaceName) {
        if (StringUtils.isBlank(interfaceName)) {
            return null;
        }
        List<TblRemoteInterfaceInfo> tblRemoteInterfaceInfoList =  tblRemoteInterfaceInfoMapper.selectList(new QueryWrapper<TblRemoteInterfaceInfo>().lambda()
                .eq(TblRemoteInterfaceInfo::getInterfaceName, interfaceName)
                .gt(TblRemoteInterfaceInfo::getAutoSort, 0)
                .orderByAsc(TblRemoteInterfaceInfo::getAutoSort));
        return CollectionUtils.isEmpty(tblRemoteInterfaceInfoList) ? null
                : tblRemoteInterfaceInfoList.stream().map(
                tblRemoteInterfaceInfo -> { return BeanUtil.copyProperties(tblRemoteInterfaceInfo, RemoteInterfaceInfo.class); }
        ).collect(Collectors.toList());

    }

    @Override
    public int updateActiveToUnavailableByInterfaceName(String interfaceName) {
        if (StringUtils.isBlank(interfaceName)) {
            return 0;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("interfaceName", interfaceName);
        return tblRemoteInterfaceInfoMapper.updateActiveToUnavailableByInterfaceName(params);
    }

    @Override
    public int updateActiveByInterfaceId(Long interfaceId, Integer active) {
        if (active == null || interfaceId == null) {
            return 0;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("interfaceId", interfaceId);
        params.put("active", active);
        return tblRemoteInterfaceInfoMapper.updateActiveByInterfaceId(params);
    }
}
